package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.NoticeMessageContentService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.NoticeMessageContentDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 龙 路 飞
 * @CreateDate: 2021-11-13 15:58
 * @Description:
 */
@RestController
@RequestMapping("/api/noticeMessageContent")
public class NoticeMessageContentController extends UserInfoController {
    @Autowired
    private NoticeMessageContentService noticeMessageContentService;

    @GetMapping("/findByUserIdList")
    public ServerResponse findByUserIdList(HttpServletRequest request) {
        NoticeMessageContentDto noticeMessageContentDto = new NoticeMessageContentDto();
        UserInfo userInfo = super.getLoginUserInfo(request);
//        noticeMessageContentDto.setUserInfo(userInfo);
        if(RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())){
            noticeMessageContentDto.setUserId(userInfo.getParentId());
        }else {
            noticeMessageContentDto.setUserId(userInfo.getId());
        }
        return ServerResponse.createBySuccess(noticeMessageContentService.findByUserIdList(noticeMessageContentDto));
    }
    @PostMapping("/edit")
    public ServerResponse edit(@RequestBody NoticeMessageContentDto noticeMessageContentDto,HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        if(RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())){
            noticeMessageContentDto.setUserId(userInfo.getParentId());
        }else {
            noticeMessageContentDto.setUserId(userInfo.getId());
        }
        return noticeMessageContentService.edit(noticeMessageContentDto);
    }

    @DeleteMapping("/delete")
    public ServerResponse delete(@RequestBody NoticeMessageContentDto noticeMessageContentDto, HttpServletRequest request){
        UserInfo userInfo = super.getLoginUserInfo(request);
        if(RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())){
            noticeMessageContentDto.setUserId(userInfo.getParentId());
        }else {
            noticeMessageContentDto.setUserId(userInfo.getId());
        }
        return noticeMessageContentService.delete(noticeMessageContentDto);
    }
}
